package com;
import a.BeanFactory;
import servlet.Dispacther;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class InitServletContextListener implements ServletContextListener{
    private BeanFactory beanFactory ;
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            beanFactory = new BeanFactory(sce.getServletContext().getInitParameter("default")) ;
            ContainerHandlerFactory handlerFactory = new ContainerHandlerFactory(beanFactory) ;
            sce.getServletContext().setAttribute(Dispacther.HANDLER,handlerFactory);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        sce.getServletContext().removeAttribute(Dispacther.DEFAULT);
        beanFactory.close();
    }
}
